//var move_step=[[2,-20,'9:00'],[40,8,'9:07'],[8,17,'9:10'],[15,2,'9:12'],[40,15,'9:15']];
var move_step=new Array();
var moveArry=new Array();
var personName="";
var personId=0;
var isMoveShow=false;
//缓存移动用到的layer
var move_layerArry=new Array();
/*function showHideMove(){
    isMoveShow=!isMoveShow;
    if(isMoveShow){
        addmoveLine(1);
    }else{
        removeAllMove();
    }
}*/


function showHistoryMove(id,type){
    personId=id;
    removeAllMove();
    isMoveShow=true;
    var obj=new Object();
    obj['id']=id;
    obj['type']=type;
    var jsonInfo=JSON.stringify(obj);
    //获取轨迹数据
    $.ajax({
        type: "POST",
        url: "dialogShowPathManage",
		data :jsonInfo,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(data){
            var resObject=data.resObject;
            //$('#persondatagriddlg').dialog('close');
            move_step=resObject.moveSteps;
            if(type==1){
                personName=resObject.goods.name;
                //personName=resObject.goods.name+"-"+resObject.goods.location;
			}
            addmoveLine(personId,type);
        },
        error: function(){
            alert("系统异常！");
        }
    });
}



/*
 * 添加移动路线上的点
 * */
function addmoveLine(id,type){
    var ld=new Array();
    // console.info(move_step);
    for(var i=0;i<move_step.length;i++){
        var ld2=new Array();
        ld2.push(move_step[i].x);
        ld2.push(move_step[i].y);
        ld2.push(move_step[i].time);
        ld2.push(move_step[i].name);
        ld.push(ld2);
    }
    var l_ptransform = ol.proj.transform(
        ld, 'EPSG:4326',
        'EPSG:4326');

    var lineFeature = new ol.Feature({//路线
        geometry: new ol.geom.LineString(l_ptransform,'XY')
    });
    var linelayers = new ol.layer.Vector({
        source: new ol.source.Vector({}),
        style: new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: '#a69bff',
                width: 2
            })
        })
    });
    linelayers.getSource().addFeature(lineFeature);

    map.addLayer(linelayers);
    move_layerArry.push(linelayers);

    for(var i=0;i<move_step.length;i++){
        if(i==0){
            addLinePoint(move_step[0].x,move_step[0].y,move_step[0].time,'start_point.png');
        }else if(i==move_step.length-1){
            addLinePoint(move_step[i].x,move_step[i].y,move_step[i].time,'end_point.png');
        }else{
            addLinePoint(move_step[i].x,move_step[i].y,move_step[i].time,'step.gif');
        }
    }
    startAnimation(type);
}

function removeAllMove(){
    for(var i=0;i<move_layerArry.length;i++){
        map.removeLayer(move_layerArry[i]);
    }
    if(pesonmove.feature){
        map.removeLayer(pesonmove.feature);
    }

}

/*
 * 添加移动路线上的点
 * */
function addLinePoint(x,y,text,pic){
    var l_ptransform = ol.proj.transform([x,y], 'EPSG:4326','EPSG:4326');
    var currentPoint = new ol.geom.Point(l_ptransform);
    var feature = new ol.Feature(currentPoint);

    var l_Layer = new ol.layer.Vector({
        source : new ol.source.Vector({})
    });

    var l_style = new ol.style.Style({
        text: new ol.style.Text({ //文本样式
            font: '14px Calibri,sans-serif',
            fill: new ol.style.Fill({
                color: '#000'
            }),
            offsetX : -5,
            offsetY : 25
        }),
        image : new ol.style.Icon(/** @type {olx.style.IconOptions} */
            ({
                opacity : 0.95,
                src : '/zhsq/userfiles/points/'+ pic
            }))
    })

    var feature = new ol.Feature(currentPoint);
    l_Layer.getSource().clear();
    l_style.getText().setText(text);
    l_Layer.getSource().addFeature(feature);
    l_Layer.setStyle(l_style);
    map.addLayer(l_Layer);

    move_layerArry.push(l_Layer);
}

/*
 * 原本使用map.on('postcompose', moveFeature);map.un('postcompose', moveFeature);
 * 进行移动渲染，然后发现，如果其他图形刷洗的时候回自动触发 postcompose 导致动画加速
 * 故，还是使用旧方法
 * */

function startAnimation(type) {
    initMoveArry();
    initPersonMove();
    movePersonMove(type);
}

var pesonmove=new Object();

function initPersonMove(){
    pesonmove.id=1;
    pesonmove.name=personName;
    pesonmove.feature=null;
    pesonmove.index=0;
}


//移动人物
function movePersonMove(type) {
    if(pesonmove && moveArry.length>0){
        //一旦有坐标检查是否有人物显示层
        var pLayer=pesonmove.feature;
        if(!pLayer)
        {
            if(isMoveShow){
                pLayer=new ol.layer.Vector({
                    source: new ol.source.Vector({})
                });
                map.addLayer(pLayer);
                pesonmove.feature=pLayer;
            }

        }else if(!isMoveShow){
            map.removeLayer(pLayer);

        }
        if(moveArry.length>0 && pesonmove.index>=moveArry.length-1){

            return;
        }
        //根据坐标集合和当前index一步步刷人物坐标

        var ptransform=ol.proj.transform(moveArry[pesonmove.index],'EPSG:4326','EPSG:4326');

        //ol.proj.transform([p.xys[p.index].x,p.xys[p.index].y],'EPSG:4326', 'EPSG:3857');
        var currentPoint = new ol.geom.Point(ptransform);


        var pstyle = new ol.style.Style({
            text: new ol.style.Text({ //文本样式
                font: '14px Calibri,sans-serif',
                fill: new ol.style.Fill({
                    color: '#000'
                }),
                stroke: new ol.style.Stroke({
                    color: '#fff',
                    width: 3
                }),
                offsetX : -5,
                offsetY : 25
            }),
            image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
                opacity: 0.95,
                offsetX : -5,
                offsetY : 5,
                //src : '/zhsq/userfiles/points/eat.gif'
                src : '/zhsq/userfiles/points/person/32X32/32X32-17-' + type + '.png'
            }))

        })
        pstyle.getText().setText(pesonmove.name);
        var feature = new ol.Feature(currentPoint);
        feature.setId(pesonmove.id);
        feature.set("name",pesonmove.name);
        feature.set("type","person");
        pLayer.getSource().clear();
        pLayer.getSource().addFeature(feature);
        pLayer.setStyle(pstyle);
//				console.info(feature);
        //如果没有到底继续移动
        if(moveArry.length>0 && pesonmove.index<moveArry.length){
            setTimeout(function(){
                movePersonMove(type);
            },100)

            pesonmove.index=pesonmove.index+1;
        }
    }
}


/*
 * 计算路径上的点，劲量做到一次移动1个单位
 * */
function initMoveArry(){
    moveArry=new Array();

    for(var i=0;i<move_step.length-1;i++){
        //首先计算2点间距离
        var l=Math.sqrt(Math.pow(move_step[i].x-move_step[i+1].x,2)+Math.pow(move_step[i].y-move_step[i+1].y,2));
        l=Math.floor(l);

        moveArry.push([move_step[i].x,move_step[i].y]);
        //将剩下的距离每次变动1分之l的距离
        if(l>0){
            var x_add=(move_step[i+1].x-move_step[i].x)/l;
            var y_add=(move_step[i+1].y-move_step[i].y)/l;

            for(var j=1;j<=l;j++){
                moveArry.push([move_step[i].x+j*x_add,move_step[i].y+j*y_add]);
            }

        }


    }

    if(move_step.length>1){
        moveArry.push([move_step[move_step.length-1][0],move_step[move_step.length-1][1]]);
    }
    //console.info(moveArry);
}

